package com.chookss.home.audit.adapter;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.home.audit.project.GroupDetailActivity;
import com.chookss.home.audit.reimbursement.ReimbursementDetailActivity;
import com.chookss.home.audit.shop.ShopDetailActivity;
import com.chookss.home.audit.shop.ShopServiceDetailActivity;
import com.chookss.home.entity.WorkNewEntity;
import com.chookss.tools.ClearMoreZeroUtil;
import com.chookss.tools.MyEvent;
import com.chookss.tools.Utils;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ReimbursementAuditAdapter extends BaseQuickAdapter<WorkNewEntity, BaseViewHolderEx> {
    private Activity context;

    public ReimbursementAuditAdapter(int layoutResId, Activity context, @io.reactivex.annotations.Nullable List<WorkNewEntity> data) {
        super(layoutResId, data);
        this.context = context;
    }

    @Override
    protected void convert(BaseViewHolderEx helper, WorkNewEntity entity) {
        TextView tvTitle = helper.getView(R.id.tvTitle);
        TextView tvPost = helper.getView(R.id.tvPost);
        TextView tvGroup = helper.getView(R.id.tvGroup);
        TextView tvStatus = helper.getView(R.id.tvStatus);
        ImageView ivType = helper.getView(R.id.ivType);
        TextView tvInit = helper.getView(R.id.tvInit);
        TextView tvTime = helper.getView(R.id.tvTime);
        TextView tvMoney = helper.getView(R.id.tvMoney);
        LinearLayout llType = helper.getView(R.id.llType);
        LinearLayout llMoney = helper.getView(R.id.llMoney);
        LinearLayout llGroup = helper.getView(R.id.llGroup);
        LinearLayout llPost = helper.getView(R.id.llPost);
        tvTitle.setText(entity.getProjectName());
        tvTime.setText("发起时间："+entity.getCreateTime());
        if (entity.isSelect()) {
            ivType.setBackgroundResource(R.drawable.icon_fx_select);
        } else {
            ivType.setBackgroundResource(R.drawable.shape_circle_b5b5b5_20);
        }
        if (!Utils.isNull(entity.getParams().getYcRremark())) {
            tvInit.setText(entity.getParams().getYcRremark());
        } else if (!Utils.isNull(entity.getParams().getRepaymentRemark())) {
            tvInit.setText(entity.getParams().getRepaymentRemark());
        } else {
            if (Utils.isNull(entity.getParams().getRemark())) {
                tvInit.setText("无");
            } else {
                tvInit.setText(entity.getParams().getRemark());
            }
        }
        if (Utils.isNull(entity.getParams().getGroupName())) {
            llGroup.setVisibility(View.GONE);
        } else {
            llGroup.setVisibility(View.VISIBLE);
            tvGroup.setText("所属项目组：" + entity.getParams().getGroupName());
        }
        if (Utils.isNull(entity.getParams().getOrgName())) {
            llPost.setVisibility(View.GONE);
        } else {
            llPost.setVisibility(View.VISIBLE);
            tvPost.setText("所属部门：" + entity.getParams().getOrgName());
        }
        if (8 == Utils.changeNumber(entity.getWorkFlowType())||9 == Utils.changeNumber(entity.getWorkFlowType())||12 == Utils.changeNumber(entity.getWorkFlowType())||13 == Utils.changeNumber(entity.getWorkFlowType())) {
            llMoney.setVisibility(View.VISIBLE);
            if (8 == Utils.changeNumber(entity.getWorkFlowType())||13 == Utils.changeNumber(entity.getWorkFlowType())){
                int num = Utils.changeNumber(entity.getParams().getGoodsNum());
                if (num <= 0) {
                    num = 1;
                }
                if (Utils.isNull(entity.getBaoxiaoCode())) {
                    tvMoney.setText("物品金额：" + ClearMoreZeroUtil.subZeroAndDot(entity.getParams().getAmt()) + "元" + "  " + " 数量：" + num);
                } else {
                   if(entity.getBaoxiaoCode().equals("BAOX202001010001")){
                       //房屋租赁
                       tvMoney.setText("月租金：" + ClearMoreZeroUtil.subZeroAndDot(entity.getParams().getAmt()) + "元");
                   }else{
                       tvMoney.setText("物品金额：" + ClearMoreZeroUtil.subZeroAndDot(entity.getParams().getAmt()) + "元" + "  " + " 数量：" + num);
                   }
                }

            }else{
                tvMoney.setText("借款金额：" + ClearMoreZeroUtil.subZeroAndDot(entity.getParams().getAmt()) + "元");
            }
        }else  if (16 == Utils.changeNumber(entity.getWorkFlowType())||17 == Utils.changeNumber(entity.getWorkFlowType())) {
            llMoney.setVisibility(View.VISIBLE);
            if (16 == Utils.changeNumber(entity.getWorkFlowType())){
                tvMoney.setText("本次回款金额：" + ClearMoreZeroUtil.subZeroAndDot(entity.getParams().getAmt()) + "元");
            }else{
                tvMoney.setText("变更项目金额：" + ClearMoreZeroUtil.subZeroAndDot(entity.getParams().getAmt()) + "元");
            }
        }
        if (7 == Utils.changeNumber(entity.getWorkFlowType())) {
            tvStatus.setText("报销金额：" + ClearMoreZeroUtil.subZeroAndDot(entity.getParams().getAmt()) + "元");
        } else if (8 == Utils.changeNumber(entity.getWorkFlowType())) {
            if ("0".equals(entity.getAuditStatus())) {
                if ("2".equals(entity.getParams().getStatusCd())||"5".equals(entity.getParams().getStatusCd())) {
                    tvStatus.setText("状态：遗失");
                } else if ("3".equals(entity.getParams().getStatusCd())||"6".equals(entity.getParams().getStatusCd())) {
                    tvStatus.setText("状态：报废");
                }  else if ("7".equals(entity.getParams().getStatusCd())) {
                    tvStatus.setText("状态：交付");
                } else {
                    tvStatus.setText("状态：新增");
                }
            } else if ("1".equals(entity.getAuditStatus())) {
                if ("1".equals(entity.getParams().getReceiverCd())) {
                    tvStatus.setText("状态：交接中");
                } else {
                    if ("2".equals(entity.getParams().getStatusCd())||"5".equals(entity.getParams().getStatusCd())) {
                        tvStatus.setText("状态：遗失");
                    } else if ("3".equals(entity.getParams().getStatusCd())||"6".equals(entity.getParams().getStatusCd())) {
                        tvStatus.setText("状态：报废");
                    } else if ("7".equals(entity.getParams().getStatusCd())) {
                        tvStatus.setText("状态：交付");
                    } else {
                        tvStatus.setText("");
                    }
                }
            } else {
                if ("3".equals(entity.getAuditStatus())) {
                    tvStatus.setText("状态：废弃");
                } else {
                    tvStatus.setText("");
                }
            }
        } else if (9 == Utils.changeNumber(entity.getWorkFlowType())) {
            if ("0".equals(entity.getAuditStatus())) {
                if ("1".equals(entity.getParams().getRepaymentCd())) {
                    tvStatus.setText("状态：还款");
                } else {
                    tvStatus.setText("状态：借款");
                }
            } else if ("1".equals(entity.getAuditStatus())) {
                if ("1".equals(entity.getParams().getRepaymentCd())) {
                    tvStatus.setText("状态：还款");
                } else {
                    tvStatus.setText("");
                }
            } else {
                if ("3".equals(entity.getAuditStatus())) {
                    tvStatus.setText("状态：废弃");
                } else {
                    tvStatus.setText("");
                }
            }
        } else if (10 == Utils.changeNumber(entity.getWorkFlowType())) {
            tvStatus.setText("补贴金额：" + ClearMoreZeroUtil.subZeroAndDot(entity.getParams().getAmt()) + "元");
        }else if (12 == Utils.changeNumber(entity.getWorkFlowType())) {
            tvStatus.setText("状态：维修");
        }else if (13 == Utils.changeNumber(entity.getWorkFlowType())) {
            tvStatus.setText("状态：交付");
        } else if (16 == Utils.changeNumber(entity.getWorkFlowType())||17 == Utils.changeNumber(entity.getWorkFlowType())) {
            tvStatus.setText("原金额：" + ClearMoreZeroUtil.subZeroAndDot(entity.getParams().getOldAmt()) + "元");
        } else {
            tvStatus.setText("");
        }

        llType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map<String, String> map = new HashMap<>();
                map.put("ClockInAudit_position", helper.getItemPosition() + "");
                EventBus.getDefault().post(new MyEvent(map));
            }
        });
        helper.getItemView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Urls.antiShake.check()) {
                    return;
                }
                Intent intent = null;
                if (7 == Utils.changeNumber(entity.getWorkFlowType()) || 9 == Utils.changeNumber(entity.getWorkFlowType())|| 10 == Utils.changeNumber(entity.getWorkFlowType())) {
                    intent = new Intent(context, ReimbursementDetailActivity.class);
                    intent.putExtra("data", entity);
                    intent.putExtra("type", Utils.changeNumber(entity.getWorkFlowType()));
                } else if (8 == Utils.changeNumber(entity.getWorkFlowType())||13 == Utils.changeNumber(entity.getWorkFlowType())) {
                    intent = new Intent(context, ShopDetailActivity.class);
                    intent.putExtra("goodsCode", entity.getProjectCode());
                    intent.putExtra("type", Utils.changeNumber(entity.getWorkFlowType()));
                }else if (12 == Utils.changeNumber(entity.getWorkFlowType())) {
                    intent = new Intent(context, ShopServiceDetailActivity.class);
                    intent.putExtra("repairCode", entity.getProjectCode());
                }else if (16 == Utils.changeNumber(entity.getWorkFlowType())||17 == Utils.changeNumber(entity.getWorkFlowType())) {
                    intent = new Intent(context, GroupDetailActivity.class);
                    intent.putExtra("repairCode", entity.getProjectCode());
                    intent.putExtra("type",Utils.changeNumber(entity.getWorkFlowType()));
                }
                if(intent!=null){
                    context.startActivity(intent);
                }

            }
        });
    }
}
